package com.example.children.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.children.DTO.AttendanceTeacherDTO;
import com.example.children.DTO.ClassDTO;
import com.example.children.DTO.HeadToClassDTO;
import com.example.children.DTO.TeacherClassDTO;
import com.example.children.Model.Class;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ClassMapper extends BaseMapper<Class> {

    @Insert("INSERT INTO class (student_id, class_id) VALUES (#{studentId}, #{classId})")
    void insert(HeadToClassDTO headToClassDTO);


    @Insert("INSERT INTO classteacher (class_id,teacher_id,headteacher) VALUES (#{classId}, #{teacherId}, #{headteacher}) ")
    void insert_two(HeadToClassDTO headToClassDTO);


    // 查询总记录数
    @Select("SELECT COUNT(*) FROM classTo s WHERE s.class_id IS NOT NULL")
    int countTotalRecords();


    // 手动分页查询
    @Select("select c.class_id as classId, c.class_name as className, " +
            "t.id as teacherId, t.teacher_name as teacherName, t.teacher_phone as teacherPhone  " +
            "from classTo c " +
            "left join classteacher tt on tt.class_id = c.class_id " +
            "left join teacher t on tt.teacher_id = t.id " +
            "LIMIT #{pageSize} OFFSET #{offset}")
    List<ClassDTO> GetClassPage(@Param("offset") int offset, @Param("pageSize") int pageSize);



    @Insert("INSERT INTO classTo (class_id,class_name) VALUES (#{classId}, #{className})")
    void insertClass(Class ct);


    //条件查询
    @Select("select c.class_id as classId, c.class_name as className, " +
            "t.id as teacherId, t.teacher_name as teacherName, t.teacher_phone as teacherPhone  " +
            "from classto c " +
            "left join classteacher tt on tt.class_id = c.class_id " +
            "left join teacher t on tt.teacher_id = t.id " +
            "where (#{className} is null or c.class_name LIKE CONCAT('%', #{className}, '%')) "+
            "and (#{teacherName} is null or t.teacher_name LIKE CONCAT('%',#{teacherName},'%')) "+
            "and (#{teacherPhone} is null or t.teacher_phone LIKE CONCAT('%',#{teacherPhone},'%')) "
    )
    List<TeacherClassDTO> selectToClasss(TeacherClassDTO teacherClassDTO);


    @Update("UPDATE classTo SET class_name = #{className} WHERE class_id = #{classId}")
    void updateByIdMy(Class ct);


    @Delete("DELETE FROM classTo WHERE class_id = #{classId}")
    void deleteByIdMY(String classId);

    // 检查是否存在指定的教师记录
    @Select("SELECT count(1) > 0 FROM classteacher WHERE teacher_id = #{teacherId}")
    boolean IsSaveTeacher(HeadToClassDTO headToClassDTO);

    // 检查是否存在指定的学生记录
    @Select("SELECT count(1) > 0 FROM class WHERE student_id = #{studentId}")
    boolean IsSaveStudent(HeadToClassDTO headToClassDTO);

}
